package com.tianbing.adlottery.controller.user;

/**
 * Add one sentence class summary here.
 * Add class description here.
 *
 * @author conan
 * @version 1.0, 2005-12-6
 */

import org.apache.log4j.Logger;
import org.apache.oro.text.perl.Perl5Util;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.jubao.ejb.dao.UserDao;
import com.jubao.ejb.entity.User;
import com.tianbing.adlottery.util.ControllerFormBean;

/**
 * @author conan
 * 
 */
public class RegisterValidator implements Validator {
	static Logger logger = Logger.getLogger(RegisterValidator.class.getName());

	private static final String EMAIL_REGEXP = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";

	private static final String PHONE_REGEXP = "/(\\({0,1})(\\d{3})(\\){0,1})(\\s|-)*(\\d{3})(\\s|-)*(\\d{4})/";

	private UserDao userDao;
	// IPartyService partyService;

	/**
	 * 
	 */
	public RegisterValidator() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class clazz) {
		return clazz.equals(ControllerFormBean.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 *      org.springframework.validation.Errors)
	 */
	public void validate(Object command, Errors errors) {
		ControllerFormBean formbean = (ControllerFormBean) command;
		validateNames(formbean.getFirstName(), formbean.getLastName(), errors);
		ValidationUtils.rejectIfEmpty(errors, "emailAddr", "required.emailAddr", "User Id is required");
		validateEmail(formbean.getEmailAddr(), errors);

		validatePassword(formbean.getPassword(), formbean.getConfirmPassword(), errors);
	}

	private void validateCertificate(String certificate, Errors errors) {
		return;
	}

	private void validateNames(String firstName, String lastName, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "firstName", "required.firstName", "First Name is required");
		ValidationUtils.rejectIfEmpty(errors, "lastName", "required.lastName", "Last Name is required");
	}

	private void validatePassword(String password, String confirmPassword, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "password", "required.password", "Password is required");
		if (!password.equals(confirmPassword)) {
			errors.rejectValue("password", "Password is not match ConfirmPassword!", null, "Password is not match ConfirmPassword!");
		} else {
			logger.info("validatePassword Passed.");
		}
	}

	private void validatePhone(String phone, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "required.phone", "Phone is required");
		Perl5Util perl5Util = new Perl5Util();
		if (!perl5Util.match(PHONE_REGEXP, phone)) {
			errors.reject("invalid.phone", "Phone number is invalid");
		}
	}

	private void validateEmail(String email, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddr", "required.emailAddr", "E-mail is required");
		Perl5Util perl5Util = new Perl5Util();
		if (!perl5Util.match(EMAIL_REGEXP, email)) {
			logger.info("validateEmail email error,failed.");
			errors.rejectValue("emailAddr", "Email Formart Error!", null, "Email Format Error!");
		} else {
			User user = userDao.find(email);
			 if (user != null) {
			 logger.info("validateEmail email exists,failed.");
			 errors.rejectValue("emailAddr", "Email has already exists!",
			 null, "Email has already exists!");
			 } else {
			 logger.info("validateEmail email not exists, Passed.");
			 }
		}
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		RegisterValidator.logger = logger;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}